package re.jf.echangeur.ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.math.BigDecimal;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import re.fr.echangeur.xml.genere.EchangeurType;
import re.fr.echangeur.xml.genere.ParametreType;
import re.jf.echangeur.utilitaire.DisqueScanner;
import re.jf.echangeur.utilitaire.LectureEcritureXml;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class CreationFichierJFrame extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CreationFichierJFrame inst = new CreationFichierJFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	private JFrame parent;

	private JPanel jPanel1;
	private JButton bt_creationFichier;
	private JTextField jTextField_nom_utilisateur;
	private JLabel jLabel_nom_utilisateur;
	private JEditorPane jEditorPane1;
	private JLabel jLabel1;
	private JButton bt_valider;

	private JComboBox jComboBox1;

	private String path;

	public CreationFichierJFrame() {
		super();
		initGUI();
		affichageControlCreation(false);
		ouverture();
	}

	private void affichageControlCreation(boolean b) {
		bt_creationFichier.setVisible(b);
		jLabel_nom_utilisateur.setVisible(b);
		if (Boolean.FALSE.equals(b)) {
			jTextField_nom_utilisateur.setEnabled(false);
			try {
				jTextField_nom_utilisateur.setText(LectureEcritureXml
						.getPropietaire(jComboBox1.getSelectedItem()
								+ LectureEcritureXml.getCheminFichier()));
				setPath(jComboBox1.getSelectedItem()
						+ LectureEcritureXml.getCheminFichier());
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			jTextField_nom_utilisateur.setText("");
			jTextField_nom_utilisateur.setEnabled(true);
			jTextField_nom_utilisateur.setVisible(b);
		}
	}

	private void bt_validerActionPerformed(ActionEvent evt) {
		File file = new File(jComboBox1.getSelectedItem()
				+ LectureEcritureXml.getCheminFichier());

		if (file.exists()) {
			jEditorPane1.setBackground(Color.GREEN);
			affichageControlCreation(false);
		} else {
			jEditorPane1.setBackground(Color.RED);
			affichageControlCreation(true);
		}
	}

	private void fermeture() {
		parent.setVisible(true);
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent evt) {
					thisWindowClosing(evt);
				}

				@Override
				public void windowOpened(WindowEvent evt) {
					thisWindowOpened(evt);
				}
			});
			{
				jPanel1 = new JPanel();
				AnchorLayout jPanel1Layout = new AnchorLayout();
				jPanel1.setLayout(jPanel1Layout);
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				{
					jTextField_nom_utilisateur = new JTextField();
					jPanel1.add(jTextField_nom_utilisateur,
							new AnchorConstraint(276, 970, 364, 519,
									AnchorConstraint.ANCHOR_REL,
									AnchorConstraint.ANCHOR_REL,
									AnchorConstraint.ANCHOR_REL,
									AnchorConstraint.ANCHOR_REL));
					jTextField_nom_utilisateur
							.setPreferredSize(new java.awt.Dimension(173, 23));
				}
				{
					jLabel_nom_utilisateur = new JLabel();
					jPanel1.add(jLabel_nom_utilisateur, new AnchorConstraint(
							288, 488, 349, 32, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jLabel_nom_utilisateur.setText("Nom de l'utilisateur");
					jLabel_nom_utilisateur
							.setPreferredSize(new java.awt.Dimension(175, 16));
				}
				{
					bt_creationFichier = new JButton();
					jPanel1.add(bt_creationFichier, new AnchorConstraint(429,
							704, 517, 298, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					bt_creationFichier.setText("Créer le fichier ");
					bt_creationFichier.setPreferredSize(new java.awt.Dimension(
							156, 23));
					bt_creationFichier.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButton1ActionPerformed(evt);
						}
					});
				}
				{
					jEditorPane1 = new JEditorPane();
					jPanel1.add(jEditorPane1, new AnchorConstraint(177, 488,
							253, 417, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jEditorPane1.setText("");
					jEditorPane1
							.setPreferredSize(new java.awt.Dimension(27, 20));
				}
				{
					jLabel1 = new JLabel();
					jPanel1.add(jLabel1, new AnchorConstraint(181, 386, 242,
							32, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jLabel1.setText("Fichier de données");
					jLabel1.setPreferredSize(new java.awt.Dimension(136, 16));
				}
				{
					bt_valider = new JButton();
					jPanel1.add(bt_valider, new AnchorConstraint(47, 605, 135,
							199, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					bt_valider.setText("Séléction disque");
					bt_valider
							.setPreferredSize(new java.awt.Dimension(156, 23));
					bt_valider.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							bt_validerActionPerformed(evt);
						}
					});
				}
				{
					ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
							DisqueScanner.getListeNomLecteur());
					jComboBox1 = new JComboBox();
					jPanel1.add(jComboBox1, new AnchorConstraint(47, 167, 135,
							32, AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL,
							AnchorConstraint.ANCHOR_REL));
					jComboBox1.setModel(jComboBox1Model);
					jComboBox1.setPreferredSize(new java.awt.Dimension(52, 23));
				}
			}
			pack();
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void jButton1ActionPerformed(ActionEvent evt) {
		try {
			File chemin = new File(jComboBox1.getSelectedItem()
					+ LectureEcritureXml.getChemin());
			if (!chemin.exists()) {
				System.out.println("creation repertoire : " + chemin.mkdirs());
			} else if (!chemin.isDirectory()) {
				System.out.println("creation repertoire : " + chemin.mkdirs());
			}
			EchangeurType echangeurType = new EchangeurType();
			ParametreType parametre = new ParametreType();
			parametre.setUtilisateur(jTextField_nom_utilisateur.getText());
			parametre.setNombreFichier(new BigDecimal(0));
			echangeurType.setParametre(parametre);
			LectureEcritureXml.ecriture(echangeurType, jComboBox1
					.getSelectedItem()
					+ LectureEcritureXml.getCheminFichier());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void ouverture() {
		if (parent != null) {
			parent.setVisible(false);
		}
	}

	public void setParent(JFrame parent) {
		this.parent = parent;
	}

	private void thisWindowClosing(WindowEvent evt) {

		fermeture();
	}

	private void thisWindowOpened(WindowEvent evt) {
		ouverture();
	}

  public void setPath(String path) {
    this.path = path;
  }

  public String getPath() {
    return path;
  }
}
